﻿using Shop.BLL;
using Shop.Models;
using Shop.MVC.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.MVC.Controllers
{
    public class AccountController : Controller
    {
        BaseManager<Users> userManager = new BaseManager<Users>();
        // 注册
        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Register(UserRegVM model)
        {
            if (ModelState.IsValid)
            {
                var user = new Users()
                {
                    Name = model.Name,
                    Password = model.Password,
                    Phone = model.Phone,
                    State = 1
                };
                     
                userManager.Add(user);
                return Content("<script>alert('注册成功！');location.href='/account/login';</script>");
            }
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(UserLoginVM model)
        {
            if (ModelState.IsValid)
            {
                var where = "Phone=@phone and Password=@pwd";
                SqlParameter[] sp =
                {
                    new SqlParameter("@phone",model.Phone),
                    new SqlParameter("@pwd",model.Password)
                };

                var user=userManager.GetList(where,sp).FirstOrDefault();
                if (user != null)
                {
                    Session["User"]=user;
                    return RedirectToAction("Index", "home");
                }
                ModelState.AddModelError("", "账号或密码错误！");
            }
            return View();
        }



    }
}